#include <iostream>
#include <vector>
#define maxn 100005
using namespace std;

using LL = long long;

int a[maxn], c[maxn];
char d[maxn][20];

int fixed_len;
inline int min_a(const vector<int>& v)
{
    int res = 2147483647;
    for(int x: v)
    {
        if(x >= fixed_len) return 0;
        if(a[x] < res) res = a[x];
    }
    return res;
}

vector<int> possible_winners(int l, int r, int round)
{
    if(l == r) return vector<int>(1, l);
    if(l >= fixed_len)
    {
        vector<int> res;
        for(int i=l; i<=r; i++)
            res.push_back(i);
        return res;
    }
    int mid = l + r >> 1;
    vector<int> v1 = possible_winners(l, mid, round - 1),
                v2 = possible_winners(mid + 1, r, round - 1);
    if(d[round][l >> round] == '1')
        v1.swap(v2);
    vector<int> res;
    if(min_a(v1) < r)
        res = v2;
    for(int x: v1)
        if(x >= fixed_len || a[x] > r)
            res.push_back(x);
    return res;
}

inline int ceil_lg2(int n)
{
    int k = 0;
    while((1 << k) < n) k ++;
    return k;
}

int main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for(int i=0; i<n; i++)
        cin >> a[i];
    for(int i=0; i<m; i++)
        cin >> c[i];
    for(int i=1, k=ceil_lg2(n); i<=k; i++)
        cin >> d[i];
    int T;
    cin >> T;
    while(T--)
    {
        int msk[4];
        cin >> msk[3] >> msk[0] >> msk[1] >> msk[2];
        for(int i=0; i<n; i++)
            a[i] ^= msk[i & 3];
        LL ans = 0LL;
        for(int i=0; i<m; i++)
        {
            fixed_len = c[i];
            int k = ceil_lg2(fixed_len);
            vector<int> res = possible_winners(0, (1 << k) - 1, k);
            LL tot = 0LL;
            for(int x: res) tot += ++x;
            ans ^= (i + 1LL) * tot;
        }
        cout << ans << '\n';
        for(int i=0; i<n; i++)
            a[i] ^= msk[i & 3];
    }
    return 0;
}